//-----------------------------------------------------------------------

// <copyright file="FieldDefinitionList.cs" company="Jamie Knight">

// Copyright (c) Jamie Knight. All rights reserved.

// </copyright>

//-----------------------------------------------------------------------

using DevKnightlie.WPF.AutoForm.FieldDefinitions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DevKnightlie.WPF.AutoForm
{
    /// <summary>
    /// List of BaseFieldDefinition objects
    /// </summary>
    public class FieldDefinitionList:List<BaseFieldDefinition>
    {

        #region Methods

        /// <summary>
        /// Inserts the given field D after the named property
        /// </summary>
        /// <param name="fieldDefinition">New field D to insert</param>
        /// <param name="afterPropertyName">The property to precede the new one</param>
        public void InsertAfterProperty(BaseFieldDefinition fieldDefinition,
            String afterPropertyName)
        {
            int index = 0;
            bool found = false;
            while (index<Count && !found)
            {
                BaseFieldDefinition current = this[index];
                if (current.PropertyName.Equals(afterPropertyName))
                {
                    Insert(index + 1, fieldDefinition);
                    found = true;
                }
            }
            if (!found)
            {
                Insert(Count, fieldDefinition);
            }
        }


        /// <summary>
        /// Returns the field definition associated with the given property name
        /// </summary>
        /// <param name="propertyName">Property name to locate</param>
        /// <returns>BaseFieldDefinition</returns>
        public BaseFieldDefinition DefinitionForPropertyName(String propertyName)
        {
            BaseFieldDefinition result = null;
            result = this.First(d => d.PropertyName.Equals(propertyName));
            return result;
        }

        #endregion

    }
}
